package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.finance.FinanceGpAppRecord;
import com.ucredit.babel.common.model.third.finance.GpAppData;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 融慧金科-714标签
 *
 * @author renzhanzheng
 */

public class FinanceGpAppParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 融慧金科-714标签
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        FinanceGpAppRecord obj = (FinanceGpAppRecord)model;
        GpAppData data = obj.getData();
        JSONObject info = obj.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject result = info.getJSONObject("data") == null ? new JSONObject() : info.getJSONObject("data");

        StringBuilder sb = getBaseInfo(obj, successful);
        //状态码
        sb.append(info.getString("status") == null ? "" : info.getString("status")).append(CRT001);
        //结果描述
        sb.append(info.getString("msg") == null ? "" : info.getString("msg")).append(CRT001);
        //请求唯一ID
        sb.append(info.getString("reqId") == null ? "" : info.getString("reqId")).append(CRT001);
        //匹配类型
        sb.append(result.getString("matchTag") == null ? "" : result.getString("matchTag")).append(CRT001);
        //是否山寨机
        sb.append(result.getString("isFakePhone") == null ? "" : result.getString("isFakePhone")).append(CRT001);
        //是否命中高危信贷人群
        sb.append(result.getString("isHit") == null ? "" : result.getString("isHit")).append(CRT001);
        //当前使用高危信贷APP指数
        sb.append(result.getString("crtInstallLevel") == null ? "" : result.getString("crtInstallLevel")).append(CRT001);
        //当前卸载高危信贷APP指数
        sb.append(result.getString("crtUninstallLevel") == null ? "" : result.getString("crtUninstallLevel")).append(CRT001);
        //近1个月新安装高危信贷APP指数
        sb.append(result.getString("lmInstallLevel") == null ? "" : result.getString("lmInstallLevel")).append(CRT001);
        //近1个月新卸载高危信贷APP指数
        sb.append(result.getString("lmUninstallLevel") == null ? "" : result.getString("lmUninstallLevel")).append(CRT001);
        //近3个月新安装高危信贷APP指数
        sb.append(result.getString("lm3InstallLevel") == null ? "" : result.getString("lm3InstallLevel")).append(CRT001);
        //近3个月新卸载高危信贷APP指数
        sb.append(result.getString("lm3UninstallLevel") == null ? "" : result.getString("lm3UninstallLevel")).append(CRT001);
        //近6个月新安装高危信贷APP指数
        sb.append(result.getString("lm6InstallLevel") == null ? "" : result.getString("lm6InstallLevel")).append(CRT001);
        //近6个月新卸载高危信贷APP指数
        sb.append(result.getString("lm6UninstallLevel") == null ? "" : result.getString("lm6UninstallLevel")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_ronghui_714label_inc";
    }

    @Override
    public String getType() {
        return RecordType.FINANCE_GPAPP_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "融慧金科-714标签";
    }
}
